﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SYH.Utils;


namespace admin.syh876.com.Code
{
    public class UserAuthorizeAttribute : AuthorizeAttribute
    {
        public AdminUser AdminUser { get; private set; }

        public string AuthorizeName;
        public UserAuthorizeAttribute()
        { }
        public UserAuthorizeAttribute(string name)
        {
            this.AuthorizeName = name;
        }
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            var user = IdentityHelper.AdminUser;

            // 用户为空，赋予Guest
            if (user == null)
            {
                //HttpContext.Current.Response.Redirect("/", true);
                //filterContext.RequestContext.HttpContext.Response.Write("无权访问");
                JsHelper.Redirect("/");
                HttpContext.Current.Response.End();
                //filterContext.RequestContext.HttpContext.Response.Write();
            }

            var controller = filterContext.RouteData.Values["controller"].ToString().ToLower();
            var action = filterContext.RouteData.Values["action"].ToString().ToLower();
            //object[] noarr = filterContext.ActionDescriptor.GetCustomAttributes(typeof(NoAuthorizeAttribute), true);
            object[] namearr = filterContext.ActionDescriptor.GetCustomAttributes(typeof(UserAuthorizeAttribute), true);
            if (namearr.Length > 0)
            {
                UserAuthorizeAttribute nameatt = namearr[0] as UserAuthorizeAttribute;
                AuthorizeName = nameatt.AuthorizeName;
            }

            bool isAllowed = true;

            //if (noarr.Length > 0)
            //    isAllowed = true;

            isAllowed = this.IsAllowed(user, controller, string.IsNullOrWhiteSpace(AuthorizeName) ? action : AuthorizeName);

            if (!isAllowed)
            {
                //filterContext.RequestContext.HttpContext.Response.Write("无权访问");
                //filterContext.RequestContext.HttpContext.Response.End();
                //HttpContext.Current.Response.Redirect("/admin/", true);
                SYH.Utils.JsHelper.Redirect("/admin/");
                HttpContext.Current.Response.End();
                filterContext.Result = new EmptyResult();
            }

        }

        /// <summary>
        /// 判断是否允许访问
        /// </summary>
        /// <span name="user"> </span>用户
        /// <span name="controller"> </span>控制器
        /// <span name="action"> </span>action
        /// <returns>是否允许访问</returns>
        public bool IsAllowed(AdminUser user, string controller, string action)
        {
            bool findResult = false;
            //加载角色
            if (user != null)
            {
                findResult = FindAllowAction(user.ActionCollection, controller.ToLower(), action.ToLower());
            }

            if (findResult)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool FindAllowAction(string actionCollection, string controllerName, string actionName)
        {
            var result = false;

            if (string.IsNullOrEmpty(actionCollection))
            {
                return result;
            }

            var temp = "|" + actionCollection + "|";
            if (temp.ToLower().Contains("|" + controllerName + "," + actionName + "|"))
            {
                result = true;
            }
            return result;
        }
    }
}